window.addEventListener("load", function(e)
{
	dislikesBG.browser = 'Firefox';
	dislikesBG.loadLibraries(e);	
	dislikesBG.loadTranslations();
	dislikesBG.loadAds();	
	dislikesBG.checkStatus();
	dislikesBG.sessionChkTimeout = setInterval(function() { dislikesBG.checkStatus(); }, 30000);
}, false);

dislikesBG.checkIfFacebookPage = function(e)
{
	try
	{
		var doc = e.originalTarget;
		var currentPageURI = doc.location.href;

		if(typeof(currentPageURI) == 'undefined' || currentPageURI == '')
			return false;
		
		var re = new RegExp('^(?:f|ht)tp(?:s)?\://([^/]+)', 'im');
		var domain = currentPageURI.match(re)[1].toString();
		
		
		if(domain.indexOf('facebook.com') == -1)	return;
		
		var topFrame = dislikes.jQuery.trim(doc.defaultView.top.location.toString());
		var botFrame = dislikes.jQuery.trim(doc.defaultView.self.location.toString());
		
		if(topFrame == botFrame)
		{
			if(dislikes.jQuery("#logout_form", doc).length > 0)
			{
				dislikes.processRequest({"action": "stateChanged", "loggedIn": true});
			}
			else if(dislikes.jQuery("#login_form", doc).length > 0)
			{
				dislikes.processRequest({"action": "stateChanged", "loggedIn": false});
			}
			
			dislikes.DOMstartup(doc);
		}
	}
	catch(e)
	{
		//alert(e);
	}
};

dislikesBG.loadLibraries = function(context)
{
	(function(loader)
	{
		loader.loadSubScript("chrome://dislikes/content/scripts/jquery-1.4.2.min.js");
	})
	(
		Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
					.getService(Components.interfaces.mozIJSSubScriptLoader)
	);

	var jQuery = window.jQuery.noConflict(true);
	if( typeof(jQuery.fn._init) == 'undefined') { jQuery.fn._init = jQuery.fn.init; }
	dislikes.jQuery = dislikesBG.jQuery = jQuery;
	
	gBrowser.addEventListener("load", function (e)
	{
		dislikesBG.checkIfFacebookPage(e);
	}, true);
};

dislikesBG.escapeHTML = function(str)
{
	return str.replace(/[&"<>]/g, function (m) "&" + ({ "=&": "amp", '"': "quot", "<": "lt", ">": "gt" })[m] + ";");
};

dislikesBG.HTMLParser = function(aHTMLString)
{
	var html = document.implementation.createDocument("http://www.w3.org/1999/xhtml", "html", null),
	body = document.createElementNS("http://www.w3.org/1999/xhtml", "body");
	html.documentElement.appendChild(body);
	
	//aHTMLString = dislikesBG.escapeHTML(aHTMLString);

	body.appendChild(Components.classes["@mozilla.org/feed-unescapehtml;1"]
		.getService(Components.interfaces.nsIScriptableUnescapeHTML)
		.parseFragment(aHTMLString, false, null, body));

	return body;
};

dislikesBG.getBGurl = function(file)
{
	return 'chrome://dislikes/content/skin/'+file;
};

dislikesBG.loadOptions = function(callback, userID, doc)
{
	var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                    .getService(Components.interfaces.nsIPrefService);
	prefs = prefs.getBranch("extensions.dislikes.");
	
	try {	var lang = prefs.getCharPref(userID+'_lang');	dislikesBG.options.language = lang;	} catch(e) {}
	try {	var firstTime = prefs.getIntPref(userID+'_firstTime');	dislikesBG.options.firstTime = firstTime;	} catch(e) {}
	try {	var lastNotif = prefs.getCharPref(userID+'_lastNotif'); dislikesBG.options.lastNotif = lastNotif;	} catch(e) {}
	try {	var commentStuff = prefs.getCharPref(userID+'_commentStuff');	dislikesBG.options.commentStuff = commentStuff;	} catch(e) {}
	
	var sendOpt = dislikesBG.jQuery.extend({}, dislikesBG.options, {trans: dislikesBG.transObj, friends: dislikesBG.friendsData});
	callback(sendOpt, doc);
};

dislikesBG.saveOptions = function(callback, userID, newOptions, doc)
{
	dislikesBG.options = dislikesBG.jQuery.extend({}, newOptions);
	
	var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                    .getService(Components.interfaces.nsIPrefService);
	prefs = prefs.getBranch("extensions.dislikes.");
	
	try {	prefs.setCharPref(userID+'_lang', dislikesBG.options.language); } catch(e)	{}
	try {	prefs.setIntPref(userID+'_firstTime', dislikesBG.options.firstTime); } catch(e)	{}
	try {	prefs.setCharPref(userID+'_lastNotif', dislikesBG.options.lastNotif); } catch(e)	{}
	try {	prefs.setCharPref(userID+'_commentStuff', dislikesBG.options.commentStuff);	} catch(e)	{}
	
	callback();
};